package jtk.project4.fleet.screen.partUsed;


import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.AddPartUsedAction;
import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelPartUsedAction;
import jtk.project4.fleet.action.SavePartUsedAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.event.AddPartUsedEvent;
import jtk.project4.fleet.field.AddPartUsedField;
import jtk.project4.fleet.field.GeneralInformationField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class PartUsedView extends FormView{
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	private Action submitAction;
	private Action cancelAction;
	//private Action addPartUsedAction;
	
	public void handleEvent(AddPartUsedEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SAVE:
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				errors.get(0).getField().focus();
				break;
			}else {
				//System.out.print("view");
				submit();
				close();
			}

		default:
			propagateEvent(evt);
			break;
		}
	}
	
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createField();
		createLayout();
		createAction();
	}
	
	private void createAction() {
		//submitAction = new SubmitAction();
		//cancelAction = new CancelAction();
		//setOnChangeAction(submitAction);
		submitAction = new SavePartUsedAction();
		cancelAction = new CancelPartUsedAction();
		//addPartUsedAction = new AddPartUsedAction();
	}
	
	private void createField(){
		//new GeneralInformationField("partHist");
		submitButton = new PushButton(new SavePartUsedAction());
		cancelButton = new PushButton(new CancelPartUsedAction());		
	}
	
	private void createLayout(){
		setHeader(createHeader());
		addField(createForm());
		
	}
	
	private FormHeader createHeader(){
		formHeader = new FormHeader();
		formHeader.setTitle("addPartUsed.header.form");
		return formHeader;
	}
	
	private GroupControl createForm(){
		GroupControl form = new GroupControl();
		form.addField(new AddPartUsedField("PartHist"),NORTH);
		form.addField(createButtons(), SOUTH);
		return form;
	}
	
	private GroupControl createButtons(){
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT)); //yg tampil cm cancel button
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
}
